package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yl.auth.model.entity.JiaShu;
import com.yl.auth.model.entity.Laoren;
import com.yl.auth.service.JiaShuService;
import com.yl.auth.service.LaorenService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author L
 * @since 2023-06-16
 */
@Api(tags = "市场销售-潜在客户家属信息管理")
@RestController
@RequestMapping("/admin/auth/jiashu")
public class JiaShuController {
    @Resource
    private JiaShuService jiaShuService;

    @Resource
    private LaorenService laorenService;

    @ApiOperation(value = "老人外键为0的家属信息")
    @GetMapping("/findAll")
    public Result findAll(){
        LambdaQueryWrapper<JiaShu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(JiaShu::getLaorenId,0);
        List<JiaShu> list = jiaShuService.list(lambdaQueryWrapper);
        return Result.ok(list);
    }
    @DeleteMapping("/delete")
    public Result delete(){
        LambdaQueryWrapper<JiaShu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(JiaShu::getLaorenId,0);
        jiaShuService.remove(lambdaQueryWrapper);
        return Result.ok();
    }
    @ApiOperation(value = "根据潜在客户Id查询指定的家属信息")
    @GetMapping("/findAllAndPage/{id}")
    public Result findAllAndPage(@PathVariable Long id){
        Laoren laoren = laorenService.getById(id);
        LambdaQueryWrapper<JiaShu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JiaShu::getLaorenId,id);
        List<JiaShu> list = jiaShuService.list(wrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("laoren",laoren);
        map.put("list",list);
        return Result.ok(map);
    }
    @ApiOperation(value = "根据潜在客户Id新增家属信息")
    @PostMapping("/save/{id}")
    public Result save(@RequestBody JiaShu jiaShu,@PathVariable Long id){
        jiaShu.setLaorenId(id);
        jiaShuService.save(jiaShu);
        return Result.ok();
    }
    @ApiOperation(value = "新增家属信息")
    @PostMapping("/insert")
    public Result insert(@RequestBody JiaShu jiaShu){
        jiaShuService.save(jiaShu);
        return Result.ok();
    }
    @ApiOperation("根据Id获取指定家属信息")
    @GetMapping("/getById/{id}")
    private Result getById(@PathVariable Integer id){
        JiaShu jiaShu = jiaShuService.getById(id);
        return Result.ok(jiaShu);
    }

    @ApiOperation("根据老人Id新增家属信息")
    @PostMapping("/saveLaoren/{id}")
    public Result saveLaoren(@RequestBody JiaShu jiaShu,@PathVariable Long id) {
        if(jiaShu.getId()!=null) {
            jiaShuService.updateById(jiaShu);
        }else {
            jiaShu.setLaorenId(id);
            jiaShuService.save(jiaShu);
        }
        return  Result.ok();
    }

    @ApiOperation(value = "编辑家属信息")
    @PostMapping("/update")
    public Result update(@RequestBody JiaShu jiaShu){
        jiaShuService.updateById(jiaShu);
        return Result.ok();
    }

    @ApiOperation(value = "删除指定家属信息")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id){
        jiaShuService.removeById(id);
        return Result.ok();
    }

}

